/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.cloudbae.listener;

import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import cn.cloudbae.service.JobLifeCycleService;

import org.apache.log4j.Logger;

public class JobEventListener implements ElasticJobListener{

    Logger logger = Logger.getLogger(JobEventListener.class);
    
    String zkServiceList;
    
    String jobNamespace;
    
    ZookeeperRegistryCenter zookeeperRegistryCenter;
    
    JobLifeCycleService jobScheduleService;

    public JobEventListener(String zkServiceList, String jobNamespace, ZookeeperRegistryCenter zookeeperRegistryCenter, JobLifeCycleService jobScheduleService) {
        this.zkServiceList = zkServiceList;
        this.jobNamespace = jobNamespace;
        this.zookeeperRegistryCenter = zookeeperRegistryCenter;
        this.jobScheduleService = jobScheduleService;
    }
    
    @Override
    public void beforeJobExecuted(ShardingContexts shardingContexts) {
    }

    @Override
    public void afterJobExecuted(ShardingContexts shardingContexts) {
        String jobName = shardingContexts.getJobName();
        String param = shardingContexts.getJobParameter();
        if("Custom".equals(param)){
            jobScheduleService.removeJob(jobName);
            jobScheduleService.unregisterBean(jobName);
            logger.info("========REMOVE JOB:" + jobName);
        }
    }
}
